import requests
from First.DataAccess import utils
from First import views

def yonghuqushi(start_date, end_date):
    def hanneihuoqushuju(start_date, end_date):
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'newuser/a'
        # metrics='new_user_count,user_count'
        metrics = "new_user_count,old_user_count,new_user_ratio,old_user_ratio,brand_old_user_count,brand_old_user_ratio,user_count,session_count"
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}&start-date={start_date}&end-date={end_date}&metrics={metrics}'.format(
            access_token=access_token, appkey=appkey, method=method, start_date=start_date, end_date=end_date,
            metrics=metrics)
        return request_data(url)

    str1 = hanneihuoqushuju(start_date, end_date)
    # print(str1)
    if 'error_code' in str1:
        utils.gengxinyonghujiapi()
        str1 = hanneihuoqushuju(start_date, end_date)
        if 'error_code' in str1:
            views.hanshurwzanting('rwyonghuqushi')
    if 'error_code' not in str1:
        length = len(str1['result']['items'][0])
        dataqingxi = {}
        dataqingxi['riqi'] = []
        dataqingxi['xinyonghu'] = []
        dataqingxi['laoyonghu'] = []
        dataqingxi['xinyonghuzhanbi'] = []
        dataqingxi['laoyonghuzhanbi'] = []
        dataqingxi['banbenlaoyonghu'] = []
        dataqingxi['banbenlaoyonghuzhanbi'] = []
        dataqingxi['qidongyonghu'] = []
        dataqingxi['qidongcishu'] = []
        for num in range(0, length):
            dataqingxi['riqi'].append(str(str1['result']['items'][0][num][0]))
            dataqingxi['xinyonghu'].append(
                0 if str1['result']['items'][1][num][0] == "--" else int(str1['result']['items'][1][num][0]))
            dataqingxi['laoyonghu'].append(
                0 if str1['result']['items'][1][num][1] == "--" else int(str1['result']['items'][1][num][1]))
            dataqingxi['xinyonghuzhanbi'].append(
                0 if str1['result']['items'][1][num][2] == "--" else int(str1['result']['items'][1][num][2]))
            dataqingxi['laoyonghuzhanbi'].append(
                0 if str1['result']['items'][1][num][3] == "--" else int(str1['result']['items'][1][num][3]))
            dataqingxi['banbenlaoyonghu'].append(
                0 if str1['result']['items'][1][num][4] == "--" else int(str1['result']['items'][1][num][4]))
            dataqingxi['banbenlaoyonghuzhanbi'].append(
                0 if str1['result']['items'][1][num][5] == "--" else int(str1['result']['items'][1][num][5]))
            dataqingxi['qidongyonghu'].append(
                0 if str1['result']['items'][1][num][6] == "--" else int(str1['result']['items'][1][num][6]))
            dataqingxi['qidongcishu'].append(
                0 if str1['result']['items'][1][num][7] == "--" else int(str1['result']['items'][1][num][7]))
        return dataqingxi

def huoyueyonghu(start_date, end_date):
    def hanneihuoqushuju(start_date, end_date):
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'activitydegree/a'
        metrics = 'user_count,daily_activity_degree,day_month_activity_degree,weekly_user_count,weekly_activity_degree,monthly_user_count,monthly_activity_degree,monthly_silent_user_count,monthly_silence_ratio,lost_user_count,lost_ratio,accumulative_user_count'
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}&start-date={start_date}&end-date={end_date}&metrics={metrics}'.format(
            access_token=access_token, appkey=appkey, method=method, start_date=start_date, end_date=end_date,
            metrics=metrics)
        return request_data(url)

    str1 = hanneihuoqushuju(start_date, end_date)
    # print(str1)
    if 'error_code' in str1:
        utils.gengxinyonghujiapi()
        str1 = hanneihuoqushuju(start_date, end_date)
        if 'error_code' in str1:
            views.hanshurwzanting('rwhuoyueyonghu')
    if 'error_code' not in str1:
        length = len(str1['result']['items'][0])
        dataqingxi = {}
        dataqingxi['riqi'] = []
        dataqingxi['qidongyonghu'] = []
        dataqingxi['rihuoyuedu'] = []
        dataqingxi['zhouhuoyueyonghu'] = []
        dataqingxi['zhouhuoyuedu'] = []
        dataqingxi['yuehuoyueyonghu'] = []
        dataqingxi['yuehuoyuedu'] = []
        dataqingxi['yuechenmoyonghu'] = []
        dataqingxi['liushiyonghu'] = []
        dataqingxi['liushilv'] = []
        dataqingxi['leijiqidongyonghu'] = []
        for num in range(0, length):
            dataqingxi['riqi'].append(str(str1['result']['items'][0][num][0]))
            dataqingxi['qidongyonghu'].append(
                0 if str1['result']['items'][1][num][0] == "--" else int(str1['result']['items'][1][num][0]))
            dataqingxi['rihuoyuedu'].append(
                0 if str1['result']['items'][1][num][1] == "--" else int(str1['result']['items'][1][num][1]))
            dataqingxi['zhouhuoyueyonghu'].append(
                0 if str1['result']['items'][1][num][2] == "--" else int(str1['result']['items'][1][num][2]))
            dataqingxi['zhouhuoyuedu'].append(
                0 if str1['result']['items'][1][num][3] == "--" else int(str1['result']['items'][1][num][3]))
            dataqingxi['yuehuoyueyonghu'].append(
                0 if str1['result']['items'][1][num][4] == "--" else int(str1['result']['items'][1][num][4]))
            dataqingxi['yuehuoyuedu'].append(
                0 if str1['result']['items'][1][num][5] == "--" else int(str1['result']['items'][1][num][5]))
            dataqingxi['yuechenmoyonghu'].append(
                0 if str1['result']['items'][1][num][6] == "--" else int(str1['result']['items'][1][num][6]))
            dataqingxi['liushiyonghu'].append(
                0 if str1['result']['items'][1][num][7] == "--" else int(str1['result']['items'][1][num][7]))
            dataqingxi['liushilv'].append(
                0 if str1['result']['items'][1][num][8] == "--" else int(str1['result']['items'][1][num][8]))
            dataqingxi['leijiqidongyonghu'].append(
                0 if str1['result']['items'][1][num][9] == "--" else int(str1['result']['items'][1][num][9]))
        return dataqingxi


def yonghuhuaxiang(start_date, end_date):
    def hanneihuoqushuju(start_date, end_date):
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'visit/attribute/f'
        metrics = 'user_count_ratio'
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}&start-date={start_date}&end-date={end_date}&metrics={metrics}'.format(
            access_token=access_token, appkey=appkey, method=method, start_date=start_date, end_date=end_date,
            metrics=metrics)
        return request_data(url)

    str1 = hanneihuoqushuju(start_date, end_date)
    # print(str1)
    if 'error_code' in str1:
        utils.gengxinyonghujiapi()
        str1 = hanneihuoqushuju(start_date, end_date)
        if 'error_code' in str1:
            views.hanshurwzanting('rwyonghuhuaxiang')
    elif (len(str1['result']['sex']['items'][0]) < 1):
        views.hanshurwzanting('rwyonghuhuaxiang')
    if ('error_code' not in str1) and (len(str1['result']['sex']['items'][0]) > 0):
        dataqingxi = {}
        dataqingxi['jiaoyu'] = []
        dataqingxi['xingbie'] = []
        dataqingxi['nianling'] = []
        dataqingxi['xingqu'] = []
        dataqingxi['hangye'] = []
        for num in range(0, len(str1['result']['sex']['items'][0])):
            dataqingxi['xingbie'].append({'name':str1['result']['sex']['items'][0][num]['name'],'value':str1['result']['sex']['items'][1][num][0]})
        for num in range(0, len(str1['result']['age']['items'][0])):
            dataqingxi['nianling'].append({'name':str1['result']['age']['items'][0][num]['name'],'value':str1['result']['age']['items'][1][num][0]})
        for num in range(0, len(str1['result']['education']['items'][0])):
            dataqingxi['jiaoyu'].append({'name':str1['result']['education']['items'][0][num]['name'],'value':str1['result']['education']['items'][1][num][0]})
        for num in range(0, len(str1['result']['interest']['items'][0])):
            dataqingxi['xingqu'].append({'name':str1['result']['interest']['items'][0][num]['name'],'value':str1['result']['interest']['items'][1][num][0]})
        for num in range(0, len(str1['result']['trade']['items'][0])):
            dataqingxi['hangye'].append({'name':str1['result']['trade']['items'][0][num]['name'],'value':str1['result']['trade']['items'][1][num][0]})
        return dataqingxi


def diyufenbu(start_date, end_date):
    def hanneihuoqushuju(start_date, end_date):
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'region/a'
        # metrics = 'session_count_district,session_count_district_ratio'
        metrics = 'session_count_district, session_count_district_ratio, new_user_count_district_ratio, user_count_district_ratio, average_session_time_district'
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}&start-date={start_date}&end-date={end_date}&metrics={metrics}'.format(
            access_token=access_token, appkey=appkey, method=method, start_date=start_date, end_date=end_date,
            metrics=metrics)
        return request_data(url)

    str1 = hanneihuoqushuju(start_date, end_date)
    # print(str1)
    if 'error_code' in str1:
        utils.gengxinyonghujiapi()
        str1 = hanneihuoqushuju(start_date, end_date)
        if 'error_code' in str1:
            views.hanshurwzanting('rwdiyufenbu')
    if 'error_code' not in str1:
        length = len(str1['result']['items'][0])
        dataqingxi = {}
        dataqingxi['diqu'] = []
        dataqingxi['qidongcishu'] = []
        dataqingxi['qidongcishuzhanbi'] = []
        dataqingxi['xinyonghufenbu'] = []
        dataqingxi['qidongyonghufenbu'] = []
        dataqingxi['pingjunshiyongshichang'] = []
        for num in range(0, length):
            dataqingxi['diqu'].append(str(str1['result']['items'][0][num][0]))
            dataqingxi['qidongcishu'].append(0 if str1['result']['items'][1][num][0] == "--" else int(
                str1['result']['items'][1][num][0]))
            dataqingxi['qidongcishuzhanbi'].append(0 if str1['result']['items'][1][num][1] == "--" else int(
                str1['result']['items'][1][num][1]))
            dataqingxi['xinyonghufenbu'].append(0 if str1['result']['items'][1][num][2] == "--" else int(
                str1['result']['items'][1][num][2]))
            dataqingxi['qidongyonghufenbu'].append(0 if str1['result']['items'][1][num][3] == "--" else int(
                str1['result']['items'][1][num][3]))
            dataqingxi['pingjunshiyongshichang'].append(0 if str1['result']['items'][1][num][4] == "--" else int(
                str1['result']['items'][1][num][4]))
        return dataqingxi


def zhongduanfenxi(start_date, end_date):
    def hanneihuoqushuju(start_date, end_date):
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'terminal/a'
        # metrics = 'accumulative_session_count,accumulative_session_count_ratio'
        metrics = 'accumulative_session_count,accumulative_new_user_count_ratio,accumulative_user_count_ratio,average_use_time'
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}&start-date={start_date}&end-date={end_date}&metrics={metrics}'.format(
            access_token=access_token, appkey=appkey, method=method, start_date=start_date, end_date=end_date,
            metrics=metrics)
        return request_data(url)

    str1 = hanneihuoqushuju(start_date, end_date)
    # print(str1)
    if 'error_code' in str1:
        utils.gengxinyonghujiapi()
        str1 = hanneihuoqushuju(start_date, end_date)
        if 'error_code' in str1:
            views.hanshurwzanting('rwzhongduanfenxi')
    if 'error_code' not in str1:
        length = len(str1['result']['items'][0])
        dataqingxi = {}
        dataqingxi['zhongduanming'] = []
        dataqingxi['qidongcishu'] = []
        dataqingxi['xinyonghuzhanbi'] = []
        dataqingxi['yonghuzhanbi'] = []
        dataqingxi['pingjunshiyong'] = []
        for num in range(0, length):
            dataqingxi['zhongduanming'].append(str(str1['result']['items'][0][num][0]))
            dataqingxi['qidongcishu'].append(
                0 if str1['result']['items'][1][num][0] == "--" else int(str1['result']['items'][1][num][0]))
            dataqingxi['xinyonghuzhanbi'].append(
                0 if str1['result']['items'][1][num][1] == "--" else int(str1['result']['items'][1][num][1]))
            dataqingxi['yonghuzhanbi'].append(
                0 if str1['result']['items'][1][num][2] == "--" else int(str1['result']['items'][1][num][2]))
            dataqingxi['pingjunshiyong'].append(
                0 if str1['result']['items'][1][num][3] == "--" else int(str1['result']['items'][1][num][3]))
        return dataqingxi


def banbenfenxi(start_date, end_date):
    def hanneihuoqushuju(start_date, end_date):
        access_token, appkey = utils.huoquyonghujiapi()
        method = 'version/a'
        # metrics = 'upgrade_user_count,brand_new_user_count'
        metrics = 'upgrade_user_count, brand_new_user_count, new_user_count_version, session_count_version, user_count_version, average_session_time_version'
        url = 'https://openapi.baidu.com/rest/2.0/mtj/svc/app/getDataByKey?access_token={access_token}&key={appkey}&method={method}&start-date={start_date}&end-date={end_date}&metrics={metrics}'.format(
            access_token=access_token, appkey=appkey, method=method, start_date=start_date, end_date=end_date,
            metrics=metrics)
        return request_data(url)

    str1 = hanneihuoqushuju(start_date, end_date)
    # print(str1)
    if 'error_code' in str1:
        utils.gengxinyonghujiapi()
        str1 = hanneihuoqushuju(start_date, end_date)
        if 'error_code' in str1:
            views.hanshurwzanting('rwbanbenfenxi')
    if 'error_code' not in str1:
        length = len(str1['result']['items'][0])
        dataqingxi = {}
        dataqingxi['banben'] = []
        dataqingxi['shengjiyonghu'] = []
        dataqingxi['xinyonghu'] = []
        dataqingxi['xinyonghufenbu'] = []
        dataqingxi['qidongcishu'] = []
        dataqingxi['qidongyonghu'] = []
        dataqingxi['bilv'] = []
        dataqingxi['pingjunshiyongshichang'] = []
        for num in range(0, length):
            dataqingxi['banben'].append(str(str1['result']['items'][0][num][0]))
            dataqingxi['shengjiyonghu'].append(
                0 if str1['result']['items'][1][num][0] == "--" else int(str1['result']['items'][1][num][0]))
            dataqingxi['xinyonghu'].append(
                0 if str1['result']['items'][1][num][1] == "--" else int(str1['result']['items'][1][num][1]))
            dataqingxi['xinyonghufenbu'].append(
                0 if str1['result']['items'][1][num][2] == "--" else int(str1['result']['items'][1][num][2]))
            dataqingxi['qidongcishu'].append(
                0 if str1['result']['items'][1][num][3] == "--" else int(str1['result']['items'][1][num][3]))
            dataqingxi['qidongyonghu'].append(
                0 if str1['result']['items'][1][num][4] == "--" else int(str1['result']['items'][1][num][4]))
            dataqingxi['bilv'].append(
                0 if str1['result']['items'][1][num][5] == "--" else int(str1['result']['items'][1][num][5]))
            dataqingxi['pingjunshiyongshichang'].append(
                0 if str1['result']['items'][1][num][6] == "--" else int(str1['result']['items'][1][num][6]))
        return dataqingxi


def request_data(url):
    req = requests.get(url, timeout=30)  # 请求连接
    req_jason = req.json()  # 获取数据
    return req_jason
